package com.gys.common.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import com.gys.common.util.BaseMapper;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;

@Configuration
@MapperScan(basePackages={"com.gys.mapper"}, sqlSessionFactoryRef="sqlSessionFactorySqlite", markerInterface=BaseMapper.class)
public class DataSourceSqliteConfig {
    static final String MAPPER_LOCATION = "com.gys.mapper";
    static final String MAPPER_XML_LOCATION = "classpath:mapper/*.xml";
    @Value(value="${second.datasource.driver-class-name}")
    private String driverClass;
    @Value(value="${second.datasource.url}")
    private String url;
    @Value(value="${second.datasource.username}")
    private String user;
    @Value(value="${second.datasource.password}")
    private String password;
    @Value(value="${spring.datasource.validationQuery}")
    private String validationQuery;

    @Bean(name={"dataSourceSqlite"})
    public DataSource dataSourceSqlite() {
        String dbUrl = "jdbc:sqlite:" + System.getProperty("user.dir") + "/gys.sqlite";
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(this.driverClass);
        dataSource.setUrl(dbUrl);
        dataSource.setUsername(this.user);
        dataSource.setPassword(this.password);
        dataSource.setValidationQuery(this.validationQuery);
        return dataSource;
    }

    @Bean(name={"transactionManagerSqlite"})
    public DataSourceTransactionManager transactionManagerSqlite() {
        return new DataSourceTransactionManager(this.dataSourceSqlite());
    }

    @Bean(name={"sqlSessionFactorySqlite"})
    public SqlSessionFactory sqlSessionFactorySqlite(@Qualifier(value="dataSourceSqlite") DataSource dataSourceSqlite) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSourceSqlite);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_XML_LOCATION));
        sessionFactory.setTypeAliasesPackage("com.gys.mapper.entity");
        PageInterceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "sqlite");
        properties.setProperty("reasonable", "false");
        properties.setProperty("supportMethodsArguments", "false");
        properties.setProperty("params", "count=countSql");
        interceptor.setProperties(properties);
        sessionFactory.setPlugins(new Interceptor[]{interceptor});
        return sessionFactory.getObject();
    }
}

